import axios from 'axios'
import qs from 'qs'

const ERR_OK = 0

export async function get(url) {
  console.log('进入util.http.get')

  let ret = null
  await axios.get(url).then((res)=>{
    ret = res.data
    const {code, data, msg} = ret
    if(code === ERR_OK) {
      console.log('http get请求成功')
      console.log(`url:${url}`)
      console.log('返回:')
      console.log(ret)
    }else {
      console.log('http get请求业务失败')
      console.log(`url:${url}`)
      console.log('返回:')
      console.log(ret)
    }
  }).catch(()=>{
    console.log('http get请求物理失败')
    console.log(`url:${url}`)
  })

  return ret
}

export async function getByParams(url) {
  return async function (params) {
    console.log('进入util.http.getByParams')
    
    let ret = null
    await axios.get(url, {
      params
    }).then((res)=>{
      ret = res
      const {code, data, msg} = ret.data
      if(code === ERR_OK) {
        console.log('http get请求成功')
        console.log(`url:${url}`)
        console.log('params:')
        console.log(params)
        console.log('返回:')
        console.log(ret.data)
      }else {
        console.log('http请求业务失败')
        console.log(`url:${url}`)
        console.log('params:')
        console.log(params)
        console.log('返回:')
        console.log(res.data)
      }
    }).catch(()=>{
      console.log('http get请求物理失败')
      console.log(`url:${url}`)
      console.log('params:')
      console.log(params)
    })

    return ret
  }
}

// axios默认使用Content-Type: application/json
// data:嵌套对象
// spring boot用@RequestBody接收
export async function postByJson(url, data) {
  console.log('进入util.http.post')

  let ret = null
  await axios.post(url, data).then((res)=>{
    ret = res.data
    const {code, data, msg} = ret
    if(code === ERR_OK) {
      console.log('http post请求成功')
      console.log(`url:${url}`)
      console.log('返回:')
      console.log(ret)
    }else {
      console.log('http post请求业务失败')
      console.log(`url:${url}`)
      console.log('返回:')
      console.log(ret)
    }
  }).catch(()=>{
    console.log('http post请求物理失败')
    console.log(`url:${url}`)
  })

  return ret
}

// axios默认使用Content-Type: application/x-www-form-urlencoded
// data:无嵌套的参数列表
// spring boot用@RequestParam接收
export async function postByForm(url, data) {
  console.log('进入util.http.post')

  let ret = null
  await axios.post( url, 
                    // 将参数对象转为字符串
                    // 例如：
                    // data={
                    //   "openid":"123",
                    //   "name":"mike"
                    // }
                    // 转换后：openid=123&name=mike
                    qs.stringify(data),
                    // 设置Content-Type
                    {headers:{
                      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
                    }}
                  ).then((res)=>{
                      // axios将返回数据封装，放在data中
                      ret = res.data
                      const {code, data, msg} = ret
                      if(code === ERR_OK) {
                        console.log('http post请求成功')
                        console.log(`url:${url}`)
                        console.log('返回:')
                        console.log(ret)
                      }else {
                        console.log('http post请求业务失败')
                        console.log(`url:${url}`)
                        console.log('返回:')
                        console.log(ret)
                      }
                    }
                  ).catch(()=>{
                    console.log('http post请求物理失败')
                    console.log(`url:${url}`)
                  })

  return ret
}

